﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text.RegularExpressions;

public static class StringExtensions
{
    public static string RenderAsHtml(string text)
    {
        text = text.Replace("\n", "<br/>");

        // http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
        // \b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&@#/%=~_|$?!:,.]*[A-Z0-9+&@#/%=~_|$]

        Regex regex = new Regex(@"http(s)?://([\w-]+\.)+[\w-]+((/)?)+[\w-./?%&=]*", 
            RegexOptions.IgnoreCase | RegexOptions.Compiled);

        MatchCollection matches = regex.Matches(text);
        
        foreach (Match match in matches)
        {
            text = text.Replace(match.Value, string.Format("<a href=\"{0}\">{0}</a>", match.Value));
        }

        return text;
    }
}
